package ArraySort;

import java.util.Comparator;

public class BubbleSort
{
    public static void bubbleSort(Comparable[]anArray)   //参数为类且参数为一个
    {
        for(int time = 0;time < anArray.length-1; time++)
        {
            for(int index = 0; index < anArray.length-1; index++)
            {

                if(anArray[index].compareTo(anArray[index+1])>0)
                {
                    interchange(index, index + 1, anArray);
                }//循环交换直到排序成功
            }
        }
    }

    public static void bubbleSort(int[]anArray)//参数为整型
    {
        for(int time = 0;time < anArray.length-1; time++)
        {
            for(int index = 0; index < anArray.length-1; index++)
            {

                if(anArray[index]>anArray[index+1])
                {
                    interchange(index, index + 1, anArray);
                }//循环交换直到排序成功
            }
        }
    }

    public static void bubbleSort(Comparable[]anArray, Comparator<Comparable> b)
    {
        for(int time = 0;time < anArray.length-1; time++)
        {
            for(int index = 0; index < anArray.length-1; index++)
            {
                if(b.compare(anArray[index],anArray[index+1])>0)
                {
                    interchange(index, index + 1, anArray);
                }
                //循环交换直到排序成功
            }
        }
    }


    private static void interchange(int i, int j, Comparable[] a)//值的交换
    {
        Comparable temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    private static void interchange(int i, int j, int[] a)//值的交换
    {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

}
